package com.dxq.test.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.dxq.test.App;
import com.dxq.test.bean.RemindEntity;
import com.dxq.test.common.Constant;
import com.dxq.test.R;
import com.dxq.test.activity.BreedActivity;
import com.dxq.test.activity.EventActivity;
import com.dxq.test.activity.FeedOperateActivity;
import com.dxq.test.activity.GroupActivity;
import com.dxq.test.activity.LivestockActivity;
import com.dxq.test.activity.MainActivity;
import com.dxq.test.activity.RemindActivity;
import com.dxq.test.activity.SettingActivity;
import com.dxq.test.activity.VaccineActivity;
import com.dxq.test.common.DBOperate;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.j256.ormlite.dao.ForeignCollection;

import java.util.ArrayList;
import java.util.List;


public class Fragment3_Event extends Fragment implements View.OnClickListener {
    private View view;// this fragment UI

    private String[] types = {"牛", "羊", "禽类"};
    private String[] strings = new String[]{"人生得意须尽欢", "莫使金樽空对月", "天生我材必有用", "千金散尽还复来"};
    private int curStr;
    private boolean threadRun = true;

    private TextView tvSize1, tvSize2, tvSize3, tvSize4, tvSize5;
    private TextView tvName1, tvName2, tvName3, tvName4, tvName5;

    private LinearLayout tvXM, tvGroup, tvSB, tvHelp;
    private LinearLayout tvSJ, tvFZ, tvPZ, tvSW, tvTX;
    private ImageView ivSetting;
    private TextSwitcher textSwitcher;


    private List<Label> selectTypeLabel = new ArrayList<>();
    private List<RemindEntity> remindList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_event, null);
        initView();
        return view;
    }

    private void initView() {
        tvSize1 = view.findViewById(R.id.tvSize1);
        tvSize2 = view.findViewById(R.id.tvSize2);
        tvSize3 = view.findViewById(R.id.tvSize3);
        tvSize4 = view.findViewById(R.id.tvSize4);
        tvSize5 = view.findViewById(R.id.tvSize5);
        tvName1 = view.findViewById(R.id.tvName1);
        tvName2 = view.findViewById(R.id.tvName2);
        tvName3 = view.findViewById(R.id.tvName3);
        tvName4 = view.findViewById(R.id.tvName4);
        tvName5 = view.findViewById(R.id.tvName5);
        textSwitcher = view.findViewById(R.id.textSwitcher);

        tvXM = view.findViewById(R.id.tvXM);
        tvGroup = view.findViewById(R.id.tvGroup);
        tvSJ = view.findViewById(R.id.tvSJ);
        tvFZ = view.findViewById(R.id.tvFZ);
        tvPZ = view.findViewById(R.id.tvPZ);
        tvSW = view.findViewById(R.id.tvSW);
        tvTX = view.findViewById(R.id.tvTX);
        ivSetting = view.findViewById(R.id.ivSetting);

        tvXM.setOnClickListener(this);
        tvGroup.setOnClickListener(this);
        tvSJ.setOnClickListener(this);
        tvFZ.setOnClickListener(this);
        tvPZ.setOnClickListener(this);
        tvSW.setOnClickListener(this);
        tvTX.setOnClickListener(this);
        ivSetting.setOnClickListener(this);

        //默认没有提醒数据

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(getContext());
                //设置最多只能显示一行
                textView.setMaxLines(1);
                textView.setTextColor(Color.RED);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.gravity = Gravity.CENTER_VERTICAL;
                lp.leftMargin = 10;
                lp.topMargin = 10;
                lp.bottomMargin = 10;
                textView.setLayoutParams(lp);
                textView.setTextSize(18f);
                return textView;
            }
        });

        textSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), RemindActivity.class));
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("循环", "开始");
                while (threadRun) {
                    ThreadUtils.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            nextTextSwitcher();
                        }
                    });
                    curStr++;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Log.i("循环", "结束");
            }
        }).start();
    }

    //事件处理函数，控制显示下一个字符串
    public void nextTextSwitcher() {
        if (!ObjectUtils.isEmpty(remindList)) {
            RemindEntity remindEntity = remindList.get(curStr % remindList.size());
            textSwitcher.setText(remindEntity.getInfo());
            //Log.i("循环", remindEntity.getInfo());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        threadRun = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
        getRemindData();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private boolean f1hidden = false;
    private String TGA = "Fragment1";

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        f1hidden = hidden;
        if (hidden) {
            //隐藏时所作的事情
        } else {
            //显示时所作的事情
            getData();
            getRemindData();
        }
    }

    //获取提醒消息
    private void getRemindData() {
        remindList.clear();
        List<RemindEntity> list = DBOperate.getRemindList(((MainActivity) getActivity()).tablesDao);
        if (!ObjectUtils.isEmpty(list)) {
            for (RemindEntity entity : list) {
                if (entity.getTimeSpan() - TimeUtils.getNowMills() > 0) {//过滤掉已过期的提醒消息
                    remindList.add(entity);
                }
            }
            if (remindList.size() == 0)
                remindList.add(new RemindEntity(Constant.NO_REMIND));
        } else {
            remindList.add(new RemindEntity(Constant.NO_REMIND));
        }
    }

    //获取牧场牲畜信息
    private void getData() {
        selectTypeLabel.clear();
        Pasture pasture = ((MainActivity) getActivity()).tablesDao.queryPastureById(App.select_pasture_id);
        if (null != pasture) {
            ForeignCollection<Label> labels = pasture.getLabels();
            for (Label label : labels) {
                selectTypeLabel.add(label);
            }
        }
        //显示牲畜详情
        int size1 = 0, size2 = 0, size3 = 0, size4 = 0, size5 = selectTypeLabel.size();
        for (Label label : selectTypeLabel) {
            if (label.getSex().equals(Constant.SEX_MAN)) {
                //公
                size1++;
            }
            if (label.getSex().equals(Constant.SEX_WOMAN)) {
                //母
                size2++;
            }
            if (label.getBreedingStatus().equals(Constant.BREEDSTATUS_CUNLAN)) {
                //存栏
                size3++;
            }
            if (label.getBreedingStatus().equals(Constant.BREEDSTATUS_CHULAN)) {
                //出栏
                size4++;
            }
        }
        tvSize1.setText(String.valueOf(size1));//公牲畜数量
        tvName1.setText(Constant.SEX_MAN + types[App.select_label_type]);
        tvSize2.setText(String.valueOf(size2));//母牲畜数量
        tvName2.setText(Constant.SEX_WOMAN + types[App.select_label_type]);
        tvSize3.setText(String.valueOf(size3));//存栏牲畜数量
        tvName3.setText(Constant.BREEDSTATUS_CUNLAN);
        tvSize4.setText(String.valueOf(size4));//出栏牲畜数量
        tvName4.setText(Constant.BREEDSTATUS_CHULAN);
        tvSize5.setText(String.valueOf(size3 + size4));//总牲畜数量
        tvName5.setText("总数");
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.tvXM:
                intent = new Intent(getActivity(), LivestockActivity.class);
                intent.putExtra("breedingStatus", Constant.BREEDSTATUS_CUNLAN);
                startActivity(intent);
                break;

            case R.id.tvGroup:
                startActivity(new Intent(getActivity(), GroupActivity.class));
                break;

            case R.id.tvSJ:
                startActivity(new Intent(getActivity(), EventActivity.class));
                break;

            case R.id.tvFZ:
                startActivity(new Intent(getActivity(), VaccineActivity.class));
                break;

            case R.id.tvPZ:
                startActivity(new Intent(getActivity(), BreedActivity.class));
                break;

            case R.id.tvSW:
                startActivity(new Intent(getActivity(), FeedOperateActivity.class));
                break;

            case R.id.tvTX:
                startActivity(new Intent(getActivity(), RemindActivity.class));
                break;

            case R.id.ivSetting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }

}
